package view.other
{
	import flash.display.DisplayObject;
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	
	import manager.LevelManange;
	
	public class LockView extends Sprite
	{
		private var lockNum:int = 20;
		
		/**
		 *每行列数 
		 */
		private const COL:int = 10;
		
		private const ROW:int = 2;
		
		private var sprite:Sprite;
		
		private var callback:Function;
		
		private var lockAry:Array;
		public function LockView(call:Function)
		{
			callback = call;
			super();
			this.x = 100;
			this.y = 200;
			lockAry=[];
			initface();
			this.addEventListener(MouseEvent.CLICK,onClick);
			this.addEventListener(MouseEvent.MOUSE_OVER,mouseOver);
			this.addEventListener(MouseEvent.MOUSE_OUT,mouseOut);
			this.addEventListener(MouseEvent.MOUSE_DOWN,mouseDown);
		}
		
		public function unlockNext():void{
			for(var i:int=0;i<lockAry.length;i++){
				if(int(lockAry[i].name) == LevelManange.getInstance().curLevel){
					lockAry[i].txt.visible = true;
					lockAry[i].mouseEnabled = true;
					lockAry[i].lock.visible = false;
				}
			}
		}
		
		private function mouseDown(e:MouseEvent):void{
			sprite.visible = false;
		}
		
		private function mouseOver(e:MouseEvent):void{
			if(e.target is Lock){
				var lock:DisplayObject = e.target as DisplayObject;
				sprite.x = lock.x ;
				sprite.y = lock.y ;
				sprite.visible = true;
			}
		}
		
		private function mouseOut(e:MouseEvent):void{
			sprite.visible = false;
		}
		
		protected function onClick(event:MouseEvent):void
		{
			if(event.target is Lock){
				LevelManange.getInstance().curLevel = int(event.target.name);
				callback();
			}
		}
		
		private function initface():void
		{
			for(var i:int = 0;i<ROW;i++){
				for(var j:int = 0;j<COL;j++){
					var lock:Lock = new Lock();
					this.addChild(lock);
					lock.x = 80*j;
					lock.y = 100*i;
					var index:int = (j+COL*i + 1);
					lock.name = index + "";
					lock.mouseChildren = false;
					lock.lock.mouseChildren = false;
					lock.lock.mouseEnabled = false;
					lock.txt.selectable = false;
					lock.buttonMode = true;
					lock.txt.text = index+"";
					if(index <= LevelManange.getInstance().userLevel){
						lock.lock.visible = false;
					}else{
						lock.mouseEnabled = false;
						lock.lock.visible = true;
						lock.txt.visible = false;
					}
					lockAry.push(lock);
				}
			}
			
			sprite = new Sprite();
			sprite.graphics.beginFill(0xffffff,0.2);
			sprite.graphics.drawCircle(0,0,22.5);
			sprite.graphics.endFill();
			this.addChild(sprite);
			sprite.visible = false;
			sprite.mouseEnabled = false;
			sprite.mouseChildren = false;
		}
	}
}